<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <button onclick="openSocket()">开启</button>
    <button onclick="sendMessage()">发送</button>
</head>
<body>

</body>
<script>
    let socket;

    function openSocket(){
        if(typeof (WebSocket)===undefined){
            console.log("您的浏览器不支持WebSocket，将不能展示进度条")
        }else {
            console.log("您的浏览器支持WebSocket")
            let socketUrl = "ws://localhost:8080/webSocket/"+12;
            console.log(socketUrl);
            if (socket != null) {
                socket.close();
                socket = null;
            }
            socket = new WebSocket(socketUrl)
            socket.onopen = function () {
                console.log("WebSocket开启")
            }
            socket.onmessage = function (msg) {
                let serverMsg = "收到服务端信息" + msg.data;
                console.log(serverMsg)
            }
            socket.onclose = function () {
                console.log("WebSocket关闭")
            }
            socket.onerror = function () {
                console.log("WebSocket发生了错误")
            }
        }
    }
    function sendMessage(){
        if(typeof (WebSocket) === undefined){
            console.log("您的浏览器不支持WebSocket")
        }else {
            socket.send("aaa");
        }
    }
</script>
</html>